package com.dfrz.bookstore.util;


import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * 发送邮件
 *
 * @author: wangmaokang
 * @email：449086935@qq.com
 * @time: 2020/9/16 18:05
 */
public class SendEmaiUtil {

    public static void sendMail(String context,String emailNum ) throws MessagingException {
        //做链接前的准备工作  也就是参数初始化
        Properties properties = new Properties();
        properties.setProperty("mail.smtp.host","smtp.qq.com");//发送邮箱服务器
        properties.setProperty("mail.smtp.port","465");//发送端口
        properties.setProperty("mail.smtp.auth","true");//是否开启权限控制
        properties.setProperty("mail.debug","true");//true 打印信息到控制台
        properties.setProperty("mail.transport","smtp");//发送的协议是简单的邮件传输协议
        properties.setProperty("mail.smtp.ssl.enable","true");
        //建立两点之间的链接
//        System.out.println("执行了2");
        Session session = Session.getInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("keyan78@qq.com","klwnsvojhvlnbege");   //这边需要修改
//                return new PasswordAuthentication("335047897@qq.com","eqkftmevelmmbiff");   //这边需要修改
            }
        });
//        System.out.println("执行了3");
        //创建邮件对象
        Message message = new MimeMessage(session);
        //设置发件人
        message.setFrom(new InternetAddress("keyan78@qq.com"));    //发件人
//        message.setFrom(new InternetAddress("335047897@qq.com"));    //发件人

        //设置收件人
        message.setRecipient(Message.RecipientType.TO,new InternetAddress(emailNum));//收件人
        //设置主题
        message.setSubject("开发测试发送邮件");
        //设置邮件正文  第二个参数是邮件发送的类型
        message.setContent(context,"text/html;charset=UTF-8");
        //发送一封邮件
        Transport transport = session.getTransport();
//            transport.connect("449086395@qq.com","ycgdkhdmeoancbai");
        Transport.send(message);
//        System.out.println("执行了");

    }}
